#pragma once
#include "imagewidget.h"
#include "logicGo.h"
class CWeiQiPaneWidget :
	public CImageWidget
{
public:
	CWeiQiPaneWidget(void){
		m_pBlackMu=NULL;
		m_pWhiteMu=NULL;
		m_bDisplayMu=FALSE;
		m_bBackGround=TRUE;

	};
	virtual ~CWeiQiPaneWidget(void){
	};

	void init(int game_size,int cell_size,int offset=0)
	{
		m_nGameSize=game_size;
		m_nCellSize=cell_size;
		m_nOffset=offset;
		m_bDisplayCursor=FALSE;
		m_lastCursorRc.dx=0;

	}

	CDragon* m_pBlackMu;
	CDragon* m_pWhiteMu;
	int m_nGameSize;
	int m_nCellSize;
	int m_nOffset;
	BOOL m_bDisplayMu;
	WRect m_lastCursorRc;
	int m_nCursorX;
	int m_nCursorY;
	int m_bDisplayCursor;

	void DrawMu(CDragon* pBlack,CDragon* pWhite)
	{
		m_pBlackMu=pBlack;
		m_pWhiteMu=pWhite;
		m_bDisplayMu=TRUE;
		InvalidateContent(0);


	}

	void RemoveMu()
	{
		m_bDisplayMu=FALSE;
//		InvalidateContent(0);
	}

	void DisplayCursor(int x,int y)
	{
		return;
#if(0)
					m_bDisplayCursor=TRUE;
	
					WRect rc;
					rc.x=x*m_nCellSize+m_nCellSize/2+m_nOffset-1-m_nCellSize/2;
					rc.y=y*m_nCellSize+m_nCellSize/2+m_nOffset-1-m_nCellSize/2;
					rc.dx=m_nCellSize;
					rc.dy=m_nCellSize;

	
					if(this->m_lastCursorRc.dx!=0)
					{
						this->m_lastCursorRc.dx++;
						this->m_lastCursorRc.dy++;
						this->InvalidateScreenArea(&(this->m_lastCursorRc));
					}
					this->m_pCanvas->DrawRectangle(&rc,0xFF);
					this->m_lastCursorRc=rc;
#endif
					//this->InvalidateClientArea(&rc);
	}

	void HideCursor()
	{
					if(this->m_lastCursorRc.dx!=0)
					{
						this->m_lastCursorRc.dx++;
						this->m_lastCursorRc.dy++;
						this->InvalidateClientArea(&(this->m_lastCursorRc));
					}
	}
	virtual int Draw(CFrameSurface* pSurface)
	{
		return CImageWidget::Draw(pSurface);
		
	}
	virtual int CustDraw(CFrameSurface* pSurface)
	{
		CImageWidget::CustDraw(pSurface);


		//Draw CELL Line
		{
			//Draw cross line
			int x=0;
			for(int y=0;y<m_nGameSize;y++)
			{
				
					int sx=x*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int sy=y*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int tx=(m_nGameSize-1)*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int ty=y*m_nCellSize+m_nCellSize/2+m_nOffset-1;				


					this->ClientToScreen(&sx,&sy);
					this->ClientToScreen(&tx,&ty);
					pSurface->DrawLine(sx,sy,tx,ty);
			}

			int y=0;
			for(int x=0;x<m_nGameSize;x++)
			{
				
					int sx=x*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int sy=y*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int tx=x*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int ty=(m_nGameSize-1)*m_nCellSize+m_nCellSize/2+m_nOffset-1;				
					this->ClientToScreen(&sx,&sy);
					this->ClientToScreen(&tx,&ty);
					pSurface->DrawLine(sx,sy,tx,ty);
			}


			WRect rc;
			rc.x=3*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=3*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);
	
			pSurface->FillRect(&rc,0x0);

			rc.x=15*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=3*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);


			rc.x=15*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=15*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);


			rc.x=3*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=15*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);


			rc.x=9*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=3*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);

			rc.x=3*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=9*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);


			rc.x=15*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=9*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);


			rc.x=9*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=15*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);



			rc.x=9*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.y=9*m_nCellSize+m_nCellSize/2+m_nOffset-1-2;
			rc.dx=4;
			rc.dy=4;
			this->ClientToScreen(&rc);			
			pSurface->FillRect(&rc,0x0);

			
		}

		if(m_bDisplayMu)
		{
			for(int x=0;x<m_nGameSize;x++)
			{
				for(int y=0;y<m_nGameSize;y++)
				{
					int px=x*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					int py=y*m_nCellSize+m_nCellSize/2+m_nOffset-1;
					WRect rc;
					rc.dx=6;
					rc.dy=6;							
					rc.x=px-3;
					rc.y=py-3;
					if(m_pBlackMu!=NULL)
					{
						if(m_pBlackMu->IsInDragon(x,y))
						{
							pSurface->FillRect(&rc,0x0);
						}
					}

					if(m_pWhiteMu!=NULL)
					{
						if(m_pWhiteMu->IsInDragon(x,y))
						{
							pSurface->FillRect(&rc,0xFFFFFF);
						}
					}
				}
			}
		}

		return 0;
	};
	BOOL CalculateCellIndex(int x,int y,int& x_index,int& y_index)
	{
		int dx=x-6;
		int dy=y-6;
		if((dx>=0 && dx<=m_nGameSize*m_nCellSize)&&
			(dy>=0 && dy<=m_nGameSize*m_nCellSize))			
		{
			x_index=dx/m_nCellSize;
			y_index=dy/m_nCellSize;
			int x_offset=dx % m_nCellSize;
			int y_offset=dy % m_nCellSize;
			if(x_offset>m_nCellSize/2) x_index++;
			if(y_offset>m_nCellSize/2) y_index++;
			if(x_index>=19) x_index=18;
			if(y_index>=19) y_index=18;
			return TRUE;
		}
		return FALSE;
	}
	virtual int HandleEvent(int evCode, int param1, int param2){
		if(evCode==MOUSE_MOVE)
		{
			if(this->m_bMouseLButtonHold==false && this->m_bEnableMouseMove==true)
			{
				WPoint pt;
				pt=*((WPoint*)param1);

				CalculateCellIndex(pt.x,pt.y,m_nCursorX,m_nCursorY);
				if(this->m_bDisplayCursor==TRUE)
				{

					WRect rc;
					rc.x=m_nCursorX*m_nCellSize+m_nCellSize/2+m_nOffset-1-m_nCellSize/2;
					rc.y=m_nCursorY*m_nCellSize+m_nCellSize/2+m_nOffset-1-m_nCellSize/2;
					rc.dx=m_nCellSize;
					rc.dy=m_nCellSize;



					this->InvalidateClientArea(&rc);
					
				}
				
				
			}
		}

	
		return 0;
	}
};
